package com.blate.dogxiao.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.blate.dogxiao.R;
import com.blate.dogxiao.bean.ConversationBean;
import com.blate.dogxiao.databinding.ItemConversationMessageBinding;
import com.bumptech.glide.Glide;

/**
 * @author Blate
 * Created in 2021/4/21
 */
public class ConversationMessageAdapter
        extends SimpleDataRecyclerViewAdapter<ConversationBean.Msg, ItemConversationMessageBinding> {

    private String mOtherAvatarUrl;
    private String mOtherNickname;
    private String mSelfAvatarUrl;
    private String mSelfNickName;

    public void setOtherAvatarUrl(String otherAvatarUrl) {
        mOtherAvatarUrl = otherAvatarUrl;
    }

    public void setOtherNickname(String otherNickname) {
        mOtherNickname = otherNickname;
    }

    public void setSelfAvatarUrl(String selfAvatarUrl) {
        mSelfAvatarUrl = selfAvatarUrl;
    }

    public void setSelfNickName(String selfNickName) {
        mSelfNickName = selfNickName;
    }

    @Override
    protected ItemConversationMessageBinding onCreateBinding(LayoutInflater inflater, ViewGroup parent, int viewType) {
        return ItemConversationMessageBinding.inflate(inflater, parent, false);
    }

    @Override
    public void onBindViewHolder(@NonNull SimpleDataHolder<ConversationBean.Msg, ItemConversationMessageBinding> holder, ConversationBean.Msg data) {
        if (data.owner == ConversationBean.Msg.SELF) {
            holder.getBinding().llRoot.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
            holder.getBinding().tvMessage.setBackgroundResource(R.drawable.ic_bubble_self);
            Glide
                    .with(holder.getBinding().civAvatar)
                    .load(mSelfAvatarUrl)
                    .into(holder.getBinding().civAvatar);
            holder.getBinding().tvNickname.setText(mSelfNickName);
            holder.getBinding().tvMessage.setTextColor(ContextCompat.getColor(holder.getBinding().getRoot().getContext(), R.color.white));
        } else {
            holder.getBinding().llRoot.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
            holder.getBinding().tvMessage.setBackgroundResource(R.drawable.ic_bubble_other);
            Glide
                    .with(holder.getBinding().civAvatar)
                    .load(mOtherAvatarUrl)
                    .into(holder.getBinding().civAvatar);
            holder.getBinding().tvNickname.setText(mOtherNickname);
            holder.getBinding().tvMessage.setTextColor(ContextCompat.getColor(holder.getBinding().getRoot().getContext(), R.color.tc2));
        }
        holder.getBinding().tvMessage.setText(data.msg);
    }

}
